<script>
const arr = [{
  num: 1
}, {
  num: 3
}, {
  num: 9
}];

const arr2 = [1, 3, 9]

Array.prototype.reduce2 = function(callback, acc) {
  let startIndex = 0
  if(typeof acc === 'undefined') {
    startIndex = 1
    // 参数1：如果没传初始值，第一次就是数组的第一项
    // 参数1：如果传了初始值，第一次就是初始值 acc
    acc = this[0]
  }
  for(let i = startIndex; i < this.length; i ++) {
    acc = callback(acc, this[i])
  }
  return acc
}


const r = arr.reduce2((acc, cur,) => acc + cur.num, 0)
console.log(r)

const r2 = arr2.reduce2((acc, cur) => acc + cur)
console.log(r2)
</script>